/**
 * 
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.pushdata;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import poolint.integration.Poolconnection;
import poolint.join.Joinoperation;
import poolint.pull.tablename.IntegratePool;
import utility.CapitalChar;

import dbmanager.DBManager;
import dbmanager.DBOperation;


/**
 * @author kubuntu
 *
 */
public abstract class Pushoperation extends Joinoperation implements Operation  
{
	String tableName; 
	String UNIQUE = "unique";	
	DBOperation integratedDB;
	String connObjId;
	String database = "database";
	
	public Pushoperation() 
	{
		
	}
	
	
	public DBOperation getConnection() 
	{
		String fieldTableId = gin.getId(fieldTable);
		String fieldTablistId = gin.getId(fieldTablist);
		String databaseId = gin.getId(database);
		String query = "SELECT d2.pv FROM " + property_details + " d JOIN (" + property_details + " d1 JOIN " + property_details + " d2 ON ";
		query = query + "(d1.mid = d2.mid)) ON (d1.pv = d.mid) ";
		query = query + "WHERE d.pv = " + tableObj + " AND d.pid = " + fieldTableId + " AND d1.pid = " + fieldTablistId + " AND d2.pid = " + databaseId;
		
		System.out.println(" QUERY : " + query);
		
		try 
		{
			ResultSet rsConObj = DBManager.getSelect(query);
			rsConObj.next();
			connObjId = rsConObj.getString("pv");	
			rsConObj.close();
		} 
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching connection Object.");
			e.printStackTrace();
		}
		Poolconnection pullCon = new Poolconnection();
		pullCon.setPoolconnection(connObjId);
		integratedDB = pullCon.getConnect();
		return integratedDB;
	}
	

	public void generateProperty() 
	{
		/**It will fetch the value from pool hash and append the column values in field hash.*/		
		Enumeration<Object> poolIte = poolHash.keys();
		while(poolIte.hasMoreElements())
		{
			String key=null;
			try 
			{
				key = poolIte.nextElement().toString();
				String Fieldvalue = ModuleHash.get(key).toString();
				String value = poolHash.get(key).toString();
				fieldHash.put(Fieldvalue, value);
			} catch (Exception e) {
			}
		}
	}
	
	/** It will fetch the unique object from the table object and send it to the specific class for getting the field value(s). **/
	public void getUniqueKey(String tableName) 
	{
		String IdUNIQUE = gin.getId(UNIQUE);
		// SELECT * FROM property_details p where mid = 8405 and pid = 8752.
	//	String query = "SELECT pv as uniquekey FROM " + property_details + " WHERE mid = " + tableObj + " AND pid = " + IdUNIQUE;
		
		//It will fetch the unique Object from the tableObject and also returns the column(s) from uniqueObject. 
		String query = "SELECT p2.pv as uniquekey FROM " + property_details + " p1 JOIN " + property_details + " p2 ON (p1.pv = p2.mid) WHERE p1.mid = " + tableObj + " AND p1.pid = " + IdUNIQUE + " AND p2.pid = " + gin.getId(column);
		ResultSet rs = DBManager.getSelect(query);
		
		LinkedList<Object> uniqueKeyList = new LinkedList<Object>(); 
		try 
		{
			//String unique = null;
			while(rs.next())
			{
				//unique = rs.getString("uniquekey");
				uniqueKeyList.add(rs.getString("uniquekey"));
			}
			rs.close();
			
			// For creating poolHash of Mod process.
			Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(poolTablename));
			poolint.push.tablename.IntegratePool integrate = (poolint.push.tablename.IntegratePool) c.newInstance();	
			integrate.setRecentTableObject(recentTableObject);
			integrate.setUniqueKeyList(uniqueKeyList);
			integrate.setTableObj(tableObj);
			poolHash.putAll(integrate.getUniqueKeyPoolHash());
			
			Vector<Object> tempVector = integrate.getUniquePoolVector();
			if(!tempVector.isEmpty())
				uniquePoolVector.addAll(tempVector);			
		} 
		catch (ClassNotFoundException e) {
			System.out.println(" integration.pushdata:: getUniqueKey:: UNIQUE KEY Not Found...");
			e.printStackTrace();
		}
		catch (Exception e) {
			System.out.println(" integration.pushdata:: getUniqueKey:: UNIQUE KEY Not Found...");
			e.printStackTrace();
		}
	}
	
	public String getTableName() 
	{
		String tableNameId = gin.getId(fieldTableName);
		String query = "SELECT pv FROM " + property_details + " WHERE mid = " + tableObj + " AND pid = " + tableNameId;
		
		System.out.println(" QUERY : " + query);
		
		try 
		{
			ResultSet rsTableName = DBManager.getSelect(query); 
			rsTableName.next();
			tableName = gin.getItem(rsTableName.getString("pv"));
			rsTableName.close();
		} 
		catch (SQLException e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching TableName ");
			e.printStackTrace();
		}
		return tableName;
	}
}
